// @flow
import React from 'react'
import { withRouter } from 'react-router'
import R from 'ramda'
import styles from './EchartMap.css'
import echarts from 'echarts/lib/echarts'
import 'echarts/extension/bmap/bmap'
import 'echarts/lib/component/title'
import 'echarts/lib/component/tooltip'
let styleJson = [{
  'featureType': 'railway',
  'elementType': 'all',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'highway',
  'elementType': 'all',
  'stylers': {
    'color': '#fdfdfd'
  }
}, {
  'featureType': 'highway',
  'elementType': 'labels',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'poi',
  'elementType': 'all',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'green',
  'elementType': 'all',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'subway',
  'elementType': 'all',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'arterial',
  'elementType': 'labels',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'label',
  'elementType': 'labels.text.fill',
  'stylers': {
    'color': '#d8d7d3'
  }
}, {
  'featureType': 'label',
  'elementType': 'labels.icon',
  'stylers': {
    'visibility': 'off'
  }
}]

type Props = {
  data: Array<Object>,
  history: Array<Object>
}
type State = {
  tipData: Object
}
class EchartMap extends React.Component {
  state: State
  props: Props
  draw: Function
  constructor (props) {
    super(props)
    this.state = {
      tipData: {}
    }
    this.draw = this.draw.bind(this)
  }
  componentDidMount () {
    this.draw(this.props)
  }
  componentWillReceiveProps (nextProps) {
    if (!R.equals(nextProps, this.props)) {
      this.draw(nextProps)
    }
  }
  draw (props) {
    const component = this
    let myChart = echarts.init(this.refs.main)
    let option = {
      bmap: {
        center: [103.65, 36.76],
        zoom: 5,
        roam: true,
        mapStyle: {
          styleJson: styleJson
        }
      },
      // visualMap: {
      //   min: 0,
      //   max: 200,
      //   calculable: true,
      //   zlevel:444,
      //   bottom:10,
      //   right:10,
      //   inRange: {
      //     color: ['#50a3ba', '#eac736', '#d94e5d']
      //   },
      //   textStyle: {
      //     color: '#fff'
      //   }
      // },
      tooltip : {
        show : true,
        // position:function (p) {
        //   return [p[0] + 10, p[1] - 10]
        // },
        formatter: function (params) {
          const region = params.data.region
          let type = ''
          if (params.data.type) {
            type = params.data.type
          }
          return '<p>客户名称<span style="margin-left:5px">' + params.data.name + '</span></p>' +
                 '<p>机组数量<span style="margin-left:5px">' + params.data.count + '</span></p>' +
                 '<p>在线机组<span style="margin-left:5px">' + params.data.onlineCount + '</span></p>' +
                 '<p>报警机组<span style="margin-left:5px">' + params.data.alarmCount + '</span></p>' +
                 '<p>所属地区<span style="margin-left:5px">' + region.city + '</span></p>' +
                 '<p>客户类型<span style="margin-left:5px">' + type + '</span></p>'
        }
      },
      series : [
        {
          name: '风场',
          type: 'scatter',
          coordinateSystem: 'bmap',
          data: props.data
          // data: []
        }
      ]
    }
    myChart.setOption(option)
    // 添加缩放控件
    let map = myChart.getModel().getComponent('bmap').getBMap()
    map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT }))
    map.addControl(new BMap.NavigationControl())

    // 点聚合
    // var MAX = props.data.length
    // var markers = []
    // var pt = null
    // var i = 0
    // for (; i < MAX; i++) {
    //   pt = new BMap.Point(props.data[i].value[0], props.data[i].value[1])
    //   var marker = new BMap.Marker(pt)
    //   markers.push(marker)
    //   marker.addEventListener('click', attribute)
    // }
    // var markerClusterer = new BMapLib.MarkerClusterer(map, { markers: markers })
    // function attribute (e) {
    //   console.log(e)
    // }

    // myChart.on('mouseover', function (params) {
    //   const footConUpIcon = document.querySelector('#footCon-up')
    //   if (footConUpIcon) {
    //     return
    //   }
    //   // component.refs.tip.style.display = 'block'
    //   component.setState({
    //     tipData: params.data
    //   })
    // })
    // myChart.on('mouseout', (params) => {
    //   component.refs.tip.style.display = 'none'
    // })
    myChart.on('click', function (params) {
      const { history } = component.props
      const location = {
        pathname: '/device-card',
        state: {
          data: params.data,
          groupId: params.data.id,
          groupName: params.data.name,
          longitude: params.data.region.longitude,
          latitude: params.data.region.latitude,
          count: params.data.count
        }
      }
      history.push(location)
    })
  }
  render () {
    const { tipData } = this.state
    return (
      <div className={styles['mainBg']}>
        {
          // 首页右下角的提示
          // <ul className={styles['hometip']} ref='tip'>
          //   <li style={{ width:240 }}>客户名称 <span>{tipData.name}</span></li>
          //   <li>机组数量 <span>{tipData.count}</span></li>
          //   <li>在线机组 <span>{tipData.onlineCount}</span></li>
          //   <li>报警机组 <span>{tipData.alarmCount}</span></li>
          //   <li style={{ width:240 }}>客户类型 <span>{tipData.type}</span></li>
          // </ul>
        }
        <div ref='main' className={styles['main']} style={{
          position: 'absolute', top: '0', left: '0', right: '0', bottom: '0'
        }} />
      </div>
    )
  }
}

export default withRouter(EchartMap)
